<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue.min.js"></script>
    <style>
        table {
            border: solid 1px;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- {{1+1}} -->


        <div id="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
                <div class="panel-body  form-inline">
                    <label for="">
                        ID:
                        <input type="text" class="form-control" v-model="id">
                    </label>
                    <label for="">
                        Name:
                        <input type="text" class="form-control" v-model="name" @keyup.113="add">
                    </label>
                    <label for="">
                        ID:
                        <!-- 在vue中使用事件绑定机制,为元素处理函数的时候,若果添加了小括号就可以给函数传参数 -->
                        <input type="button" value="添加" class="btn btn-primary" @click="add()">
                    </label>
                    <label for="">
                        搜索名称关键字:
                        
                        <input type="text" class="form-control" v-model="keywords" id="search" v-focus>
                    </label>
                </div>
            </div>
        </div>

        <br>

        <table border="1" cellpadding="5" cellspacing="0" class="table  table-bordered table-hover  table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in search(keywords)" key="item.id">
                    <td>{{item.id}}</td>
                    <td v-text="item.name"></td>
                    <td>{{item.ctime | dateFormat('yyyy-mm-dd')}}</td>
                    <td>
                        <a href="" @click.prevent="del(item.id)">删除</a>
                        <!-- @click.prevent事件默认`阻止行为 -->
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div id="app2">
<h3>{{dt  | dateFormat}}</h3>
    </div>
    <script>
        // 全局的过滤器，进行时间的格式化
        Vue.filter("dateFormat", function (dateStr, pattern="") {
            //根据给定的时间字符串，得到特定的时间
            var dt = new Date(dateStr)
            //yyyy-mm-dd
            var y = dt.getFullYear()
            var m = dt.getMonth() + 1
            var d = dt.getDate()

            //return y+'-'+m+'-'+d
            // return '${y}-${m}-${d}'
            if (pattern && pattern.toLowerCase() === 'yyyy-mm-dd') {
                // return '${y}-${m}-${d}'
               return y+'-'+m+'-'+d;
            } else {
                var hh = dt.getHours()
                var mm = dt.getMinutes()
                var ss = dt.getSeconds()

                // return '${y}-${m}-${d} ${hh}:${mm}:${ss}'
                return y+'-'+m+'-'+d+'  '+hh+':'+mm+':'+ss;
            }
        })

        //自定义全局按键修饰符
        Vue.config.keyCodes.f2=113

        //使用Vue.directive{}定义全局的指令
        //其中：参数一是指令的名称，在定义的时候，指令的前面，不需要加v- 前缀
        Vue.directive()


        var vm = new Vue({
            el: "#app",
            data: {
                id: "",
                name: "",
                keywords: "",//搜索的关键字
                list: [
                    { id: 1, name: '奔驰', ctime: new Date() },
                    { id: 2, name: '宝马', ctime: new Date() }
                ]
            },
            methods: {
                add() {
                    var car = { id: this.id, name: this.name, ctime: new Date() }
                    this.list.push(car)
                    this.id = this.name = ""
                },
                del(id) {
                    var index = this.list.findIndex(item => {
                        if (item.id = id) {
                            return true;
                        }
                    })
                    // console.log(index)
                    this.list.splice(index, 1)
                },
                search(keywords) {
                    return this.list.filter(item => {

                        if (item.name.includes(keywords)) {
                            return item;
                        }
                    })
                    return newList
                }
            }
        });
        var vm2=new Vue({
            el:"#app2",
            data:{
                dt:new Date()
            },
            methods:{},
            filters:{
                //定义私有过滤器  过滤器有两个条件  [过滤器名称 和 处理函数]
                dataFormat:function(dateStr,pattern=''){
                    var dt = new Date(dateStr)
            //yyyy-mm-dd
            var y = dt.getFullYear()
            var m = (dt.getMonth() + 1).toString().padStart(2,'0')
            var d = dt.getDate().toString().padStart(2,'0')

            //return y+'-'+m+'-'+d
            // return '${y}-${m}-${d}'
            if (pattern && pattern.toLowerCase() === 'yyyy-mm-dd') {
                return y+'-'+m+'-'+d;
            } else {
                var hh = dt.getHours().toString().padStart(2,'0')
                var mm = dt.getMinutes().toString().padStart(2,'0')
                var ss = dt.getSeconds().toString().padStart(2,'0')

                // return '${y}-${m}-${d} ${hh}:${mm}:${ss}``````'
                return y+'-'+m+'-'+d+'  '+hh+':'+mm+':'+ss;
            }
                }
            }
        })
        // document.getElementById('search').focus()
    </script>
</body>

</html>